const app = getApp()
Page({
  data: {
    sliderValue: 0, 
    statusSuggestion: '', 
    arcAngle: 0, 
    backgroundColor: '#FADADD', 
    list2: [
      { image: '/images/健康心理1.jpg' },
      { image: '/images/健康心理2.jpg' },
      { image: '/images/健康心理3.jpg' }
    ],
  },
  onSliderChange: function(e) {
    const value = e.detail.value;
    let suggestion = '';
    let arcAngle = value * 3.6; 
    if (value <= 20) {
      suggestion = '悲伤是暂时的，抱抱';
    } else if (value > 20 && value <= 40) {
      suggestion = '烦心事总会过去的，加油哦~';
    } else if (value > 40 && value <= 60) {
      suggestion = '焦虑是可以理解的，试着放松一下吧';
    } else if (value > 60 && value <= 80) {
      suggestion = '保持良好的心态，一切都会好起来的';
    } else if (value > 80 && value <= 100) {
      suggestion = '开心最重要，继续保持哦~';
    }

    let backgroundColor = '';
    if (value <= 50) {
      backgroundColor = '#FFB6C1	'; 
    } else if (value > 50 && value <= 100) {
      backgroundColor = '	#FFDEAD'; 
    }
    let bgImageSrc = '';
    let sunMoonImageSrc = '';
    if (value < 60) {
      bgImageSrc = '/images/矩形 2.png';
      sunMoonImageSrc = '/images/月亮.gif.png';
    } else {
      bgImageSrc = '/images/矩形 3.png';
      sunMoonImageSrc = '/images/深蓝色卡通插画风露营活动宣传手机海报 (1).png.png';
    }
    this.setData({
      sliderValue: value,
      statusSuggestion: suggestion,
      arcAngle: arcAngle,
      backgroundColor: backgroundColor,
      bgImageSrc: bgImageSrc,
      sunMoonImageSrc: sunMoonImageSrc
    }, () => {
      this.drawArc();
    });
  },
  drawArc: function() {
    const ctx = wx.createCanvasContext('myCanvas', this);
    const radius = 45; 
    const centerX = 50; 
    const centerY = 50;
    ctx.clearRect(0, 0, 100, 100);
    
    // 这里将颜色固定为 #7FFF00
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius, -Math.PI / 2, -Math.PI / 2 + this.data.arcAngle * Math.PI / 180); 
    ctx.strokeStyle = '#7FFF00'; // 固定颜色
    ctx.lineWidth = 10; 
    ctx.stroke(); 
    ctx.closePath();
    ctx.setFontSize(20); 
    ctx.setTextAlign('center'); 
    ctx.setTextBaseline('middle'); 
    ctx.setFillStyle('#ffffff'); 
    ctx.fillText(this.data.sliderValue, centerX, centerY); 
    ctx.draw(false, () => {
    });
},
  hslToRgb: function(h, s, l) {
    s /= 100;
    l /= 100;

    const k = n => (n + h / 30) % 12;
    const a = s * Math.min(l, 1 - l);

    const f = n => l - a * Math.max(-1, Math.min(k(n) - 3, Math.min(9 - k(n), 1)));

    return {
      r: Math.round(f(0) * 255),
      g: Math.round(f(8) * 255),
      b: Math.round(f(4) * 255)
    };
  },
  onReady: function() {
    this.drawArc(); 
  },
  navigateToDiary: function() {
    wx.navigateTo({
      url: '/pages/newDiary/newDiary',
      fail: (err) => {
        console.error('导航失败:', err);
      }
    });
  },
  
  handleButtonClick: function(event) {
    const buttonIndex = event.currentTarget.dataset.index; 
    switch (buttonIndex) {
      case '1':
        wx.navigateTo({
          url: '../about/Test/first/first'
        });
        break;
      case '2':
        wx.navigateTo({
          url: '../about/myDiary/myDiary' 
        });
        break;
      case '3':
        wx.navigateTo({
          url: '../article/my_item' 
        });
        break;
      default:
        console.error('未知的按钮索引:', buttonIndex);
    }
  }
});